<?php

class DashboardController extends Controller
{
        //public $layout      = '//layouts/column2';
        //public $menuTitle   = 'Navigasi';
        public $menu;

        public function __construct($id, $module = null) {
            parent::__construct($id, $module);
            $this->menu = array(
                array('label'=>'Posting Pekerjaan', 'url'=>array('jobhistory')),
                array('label'=>'Lamaran Pekerjaan', 'url'=>array('applicationhistory')),
                array('label'=>'Verifikasi Pekerjaan', 'url'=>array('jobverification'), 'visible'=>!Yii::app()->user->isGuest ),
                array('label'=>'Verifikasi Lamaran', 'url'=>array('applicationverification'), 'visible'=>!Yii::app()->user->isGuest),
            );
        }

	public function actionIndex()
	{
            $this->actionJobHistory();
	}

        //Handler dari URL request yang meminta histori posting (ex: root/dashboard/jobhistory)
        //Menerima masukan berupa $page dan menampilkan histori posting pekerjaan secara paginasi
        //dari user yang meminta (diambil dari facebook ID pada session).
        //Mengconstruct view "Dashboard - Posting Pekerjaan"
        function actionJobHistory($page = 1) {
            //Get the job that is posted by this user
            $job    = new Job();
            $userFb = Yii::app()->params['userFb'];
            $job->poster_id = $userFb->getId();

            //render
            $this->render('jobHistory', array("model" => $job, "userFb" => $userFb, "notifs" => $this->fetchNotifications()));
        }

        //Handler dari URL request yang meminta histori lamaran (ex: root/dashboard/applicationhistory)
        //Menerima masukan berupa $page dan menampilkan histori lamaran pekerjaan secara paginasi
        //dari user yang meminta (diambil dari facebook ID pada session).
        //Mengconstruct view "Dashboard - Lamaran Pekerjaan"
        function actionApplicationHistory($page = 1) {
            //Get application that is posted by this user
            $app    = new Application();
            $userFb = Yii::app()->params['userFb'];
            $app->user_id = $userFb->getId();

            //Render
            $this->render('applicationHistory', array('model' => $app, 'userFb' => $userFb, "notifs" => $this->fetchNotifications()));
        }

        //Handler dari URL request yang meminta verifikasi pekerjaan (ex: root/dashboard/jobverification)
        //Menerima masukan berupa $page dan menampilkan pekerjaan secara paginasi
        //yang menunggu verifikasi untuk ditampilkan.
        //Pada $_POST[`Job`] terdapat informasi pekerjaan yang akan diverifikasi,
        //Jika terdapat value pada variabel tersebut, maka pekerjaan tersebut diverifikasi.
        //Hanya admin yang dapat mengakses halaman ini.
        //Mengconstruct view "Dashboard - Verifikasi Pekerjaan"
        function actionJobVerification($page = 1) {
            //Get the job that is posted by this user
            $job    = new Job();
            $userFb = Yii::app()->params['userFb'];

            //Apply
            if(isset($_POST['Job'])) {
                $job = $job->findByPk($_POST['Job']['id']);
                $job->status = 1;
                if(!$job->save())
                        throw new CHttpException(500, 'Unable to verify job publishing. Please try again later. ' . print_r($job->getErrors(), true));
            }

            //render
            $job->unsetAttributes();
            $job->status = 0;
            $this->render('jobverification', array("model" => $job, "userFb" => $userFb, "notifs" => $this->fetchNotifications()));
        }

        //Handler dari URL request yang meminta verifikasi lamaran (ex: root/dashboard/applicationverification)
        //Menerima masukan berupa $page dan menampilkan lamaran secara paginasi
        //yang menunggu verifikasi.
        //Pada $_POST[`Job`] terdapat informasi lamaran yang akan diverifikasi,
        //Jika terdapat value pada variabel tersebut, maka lamaran tersebut diverifikasi.
        //Hanya admin yang dapat mengakses halaman ini.
        //Mengconstruct view "Dashboard - Verifikasi Lamaran"
        function actionApplicationVerification($page = 1) {
            //Get the job that is posted by this user
            $job    = new Job();
            $userFb = Yii::app()->params['userFb'];

            //Apply
            if(isset($_POST['Job'])) {
                $job = $job->findByPk($_POST['Job']['id']);
                $job->status = 4;
                if(!$job->save())
                        throw new CHttpException(500, 'Unable to verify job transaction. Please try again later.');
            }

            //render
            $job->unsetAttributes();
            $job->status = 3;
            $this->render('applicationverification', array("model" => $job, "userFb" => $userFb, "notifs" => $this->fetchNotifications()));
        }

        protected function fetchNotifications() {
            //get notif
            $curUser = Yii::app()->params['userFb']->getId();
            $arr = Notif::model()->findAll("`user_id` = $curUser");

            //delete notif
            Notif::model()->deleteAll("`user_id` = $curUser");

            return $arr;
        }

	/**
	 * @return array action filters
	 */
	public function filters()
	{
		return array(
			'accessControl', // perform access control for CRUD operations
		);
	}

	/**
	 * Specifies the access control rules.
	 * This method is used by the 'accessControl' filter.
	 * @return array access control rules
	 */
	public function accessRules()
	{
		return array(
			array('allow',  // allow all users to perform 'index' and 'view' actions
				'actions'=>array('jobhistory','applicationhistory','index'),
				'users'=>array('*'),
			),
			array('allow', // allow authenticated user to perform 'create' and 'update' actions
				'actions'=>array('jobverification','applicationverification'),
				'users'=>array('admin'),
			),
			array('deny',  // deny all users
				'users'=>array('*'),
			),
		);
	}

	// Uncomment the following methods and override them if needed
	/*
	public function filters()
	{
		// return the filter configuration for this controller, e.g.:
		return array(
			'inlineFilterName',
			array(
				'class'=>'path.to.FilterClass',
				'propertyName'=>'propertyValue',
			),
		);
	}

	public function actions()
	{
		// return external action classes, e.g.:
		return array(
			'action1'=>'path.to.ActionClass',
			'action2'=>array(
				'class'=>'path.to.AnotherActionClass',
				'class'=>'path.to.AnotherActionClass',
				'class'=>'path.to.AnotherActionClass',
				'propertyName'=>'propertyValue',
			),
		);
	}
	*/
}